package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IRegisterService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @description： 用户注册
 * @author： Yj
 * @create： 2022/01/20
 **/
@Slf4j
@Anoymous
@RestController
@RequestMapping("user")
public class RegisterController {

    @Reference(timeout = 3000,retries = 2, check = false)
    private IKaptchaService iKaptchaService;

    @Reference(timeout = 3000,retries = 2, check = false)
    private IRegisterService registerService;

    @Autowired
    LoginController loginController;

    @PostMapping("register")
    public ResponseData register(@RequestBody Map<String,String> map, HttpServletRequest request){

        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");
        String email = map.get("email");

        //验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        //获取uuid
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = response.getCode();
        //验证失败
        if (!code.equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        //用户表插入新记录
        UserRegisterRequest registerRequest = new UserRegisterRequest();
        registerRequest.setEmail(email);
        registerRequest.setUserName(userName);
        registerRequest.setUserPwd(userPwd);
        UserRegisterResponse registerResponse = registerService.register(registerRequest);
        String registerResponseCode = registerResponse.getCode();
        //注册失败
        if (!registerResponseCode.equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(registerResponse.getMsg());
        }
        //注册成功返回null
        return new ResponseUtil<>().setData(null);
    }


    @GetMapping("verify")
    public ResponseData verify(UserVerifyRequest request,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
        UserVerifyResponse verifyResponse = registerService.verify(request);
        if (!verifyResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            httpServletResponse.setHeader("refresh","0;url="+httpServletRequest.getContextPath() + "/login#/login");
            return new ResponseUtil<>().setErrorMsg(verifyResponse.getMsg());
        }
        log.info("激活成功");
        httpServletResponse.setHeader("refresh","0;url="+httpServletRequest.getContextPath() + "/login#/login");

        return new ResponseUtil<>().setData(null);
    }
}
